"""
@version:
@filename : log
@author : liuhongjie
@projectname: day02
@time: 2022/04/16
"""

"""
需求分析：
提取出这个文本里面的ip字段、时间字段、带宽字段
每个字段都是以空白作为分割   ip字段在第一个  时间字段在第四个 带宽字段在第10个
 首先将时间格式都统一转化为 2021-06-04 04:00:20  （time）
 通过ip 去解析出它的省份和运营商是什么    （requests，json）
 https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip=106.11.159.66
 isp就是运营商字段   region就是省份字段
 最后将得到的  时间、省份、运营商、带宽字段都写入文件

 2021-06-04 04:00:20,湖南,电信,1004
"""

import json
import requests
def change_time(time):
    date = time.split('/')
    time = date[2].split(':')
    date.pop()
    for i in time:
        date.append(i)
    lst2 = ["Jua","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]
    for j in date:
        if j in lst2:
            month=lst2.index(j)+1
    year = date[2]
    day = date[0]
    other = ":".join(date[3::])
    stand_time = str(year)+"-"+str(month)+'-'+str(day)+" "+other
    return stand_time
def change_json(response):

    region = response.json()['data']['region']
    isp = response.json()['data']['isp']
    return region,isp

with open("weblog.txt","r") as fp:
    for i in fp.readlines():
        lst1 = i.split(' ')
        ip = lst1[0]
        time = lst1[3][1::]
        width = lst1[9]
        response = requests.get(f" https://ip.taobao.com/outGetIpInfo?accessKey=alibaba-inc&ip={ip}")
        region = change_json(response)[0]
        isp = change_json(response)[1]
        print(change_time(time),region,isp,width)


